<template>
	<view class="discussionContainer">
		<view class="recommend">
			<view class="tagRecommend" style="width: 270rpx;padding-left: 30rpx">
				<u-tabs active-color="#101010" inactive-color="#a8a8a8" :show-bar="false" font-size="32"
					:list="recommendList" :is-scroll="false" :current="listCurrent" @change="change"></u-tabs>
			</view>
			<u-gap height="2" bg-color="#EEEEEE"></u-gap>
			<view style="margin-top: 20rpx;" v-for="(item,index) in discuList " :key="index">
				<Diary :user-info="item.compassNormalUser" :info-data="item"
					@like-change="res => likeChange(index, res)" @delete="deleteCallback(index)" />
			</view>
			<view class="u-p-b-30" v-if="(discuList.length===0 && orderType !=3) || !isLogin">
				<empty :showLine="false" :btn-string="isLogin ? '暂无讨论，写一条吧' : '登录'"
					:url="isLogin ? '/package/discussion/publish?diaryName='+jobName : '/package/login/wx_login'"></empty>
			</view>
			
				<loadMore :status="(discuList.length===0 && orderType !=3) ?'':status" />
			<!-- 发布按钮 -->
			<view class="fabu" @click="toFabu">
				<img src="https://app.zbzedu.com/static/WaChat/icon/forum/publish.png" mode="" />
			</view>
		</view>
	</view>
</template>

<script>
	import {
		request
	} from "../../util/api";
	import Diary from "../../pages/forum/components/Diary"
	import empty from "../../pages/forum/components/empty"
	import loadMore from '../../components/loadMore'
	import {
		CHECK_TOKEN
	} from '../../util/util'
	export default {
		components: {
			Diary,
			empty,
			loadMore
		},
		data() {
			return {
				userData: {},
				newMsgFlag: false,
				firstDiary: {},
				jobName: "",
				listCurrent: 0,
				status: 'loading',
				orderType: 1,
				isLogin: CHECK_TOKEN(),
				recommendList: [{
						name: "推荐"
					},
					{
						name: "最新"
					},
					{
						name: "关注"
					}
				],
				current: 0,
				discuList: [],
				pageNum: 1,
			};
		},
		onLoad(option) {
			this.jobName=option.jobName
			this.getDisList()
			this.status = 'loading'
			this.pageNum = 1
			this.discuList=[]
		},
		onPullDownRefresh() {
			this.getDisList()
			this.status = 'loading'
			this.pageNum = 1
			this.discuList = []
			console.log('下拉刷新了')
		},
		
		onReachBottom() {
			if (!this.status) {
				this.status = 'loading'
				this.getDisList()
			}
		},
		methods: {
			// 获取职业类型讨论数据
			getDisList() {
				request({
					url: "/system/CompassDiary/wxDiaryList",
					method: "post",
					data: {
						typeCode: this.orderType,
						typeName: this.jobName,
						pageNum: this.pageNum,
						pageSize: 5
					}
				}).then(res => {
					this.discuList = res.rows
					this.pageNum++
					if (this.discuList.length >= res.total) {
						this.status = 'finished'
					} else {
						this.status = ''
					}
					uni.stopPullDownRefresh()
				})
			},
			// 改变推荐类型
			change(index) {
				this.listCurrent = index
				this.orderType = index + 1
				this.status = 'loading'
				this.pageNum = 1
				this.discuList=[],
				this.getDisList()

			},
			// 点赞
			likeChange(index, data) {
			  const {likeFlag, likeNum} = data
			  this.$set(this.discuList[index], 'likeFlag', likeFlag)
			  this.$set(this.discuList[index], 'likeNum', likeNum)
			},
			// 删除讨论
			deleteCallback(index) {
			  this.discuList.splice(index, 1)
			},
			// 跳转到发布
			toFabu() {
				this.$u.route({
				   url: '/package/discussion/publish?diaryName='+this.jobName,
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.discussionContainer {
		.fabu {
			position: fixed;
			right: 28rpx;
			bottom: 130rpx;
			width: 120rpx;
			height: 120rpx;
			border-radius: 50%;
			height: 120rpx;
			z-index: 10;

			image {
				width: 100%;
				height: 100%;
			}
		}
	}
</style>
